/* 
TimerWithFlag_41.pde Copyright (c) 2010 Ed Bennett.  All rights reserved.

Using flags in loop() --first example with fixed speeds. Three LED's blinking together at different rates. Three timer channels are used. The channels are numbered 0, 1, and 2. 

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


// Timer commands usually take a channel number and/or a 
// time-value. 
//
// The unit of time is is called a blink. The duration 
// of a blink is related to an AVR jiffy. See
// http://en.wikipedia.org/wiki/Jiffy_%28time%29

#include <TimerScheduler.h>

// hardware pins
int LED0 = 13;
int LED1 = 12;
int LED2 = 11;
int LED3 = 10;
int LED4 = 9;

int outval0 = 0;
int outval1 = 0;
int outval2 = 0;
int outval3 = 0;
int outval4 = 0;

void setup()
{   
  pinMode(LED0, OUTPUT); 
  pinMode(LED1, OUTPUT); 
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);  
  pinMode(LED4, OUTPUT);
  
  Timer.begin();        // start the hardware timer
 Timer.setMasterPeriod(50);
  Timer.repeat(0,300); // (channel, time)
  Timer.repeat(1,600); 
  Timer.repeat(2,1200);
  Timer.repeat(3,2400); 
  Timer.repeat(4,4800);
  
  Timer.start(0);       // (channel)
  Timer.start(1); 
  Timer.start(2);
  Timer.start(3); 
  Timer.start(4);  
}

  // this is how to poll a timer flag
void loop()
{
  // for LED0
  if(Timer.flagIsSet(0)){  // channel 0 flag
    outval0 ^= 1;          // toggle the pin
    digitalWrite(LED0, outval0);
  }

  // for LED1   
  if(Timer.flagIsSet(1)){  // channel 1 flag
    outval1 ^= 1;
    digitalWrite(LED1, outval1);
  }

  //for LED2  
  if(Timer.flagIsSet(2)){  // channel 2 flag
    outval2 ^= 1;
    digitalWrite(LED2, outval2);
  }
  
    //for LED2  
  if(Timer.flagIsSet(3)){  // channel 3 flag
    outval3 ^= 1;
    digitalWrite(LED3, outval3);
  }
    //for LED4  
  if(Timer.flagIsSet(4)){  // channel 4 flag
    outval4 ^= 1;
    digitalWrite(LED4, outval4);
  }
  
}
